Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Class → Class Variables

Python Class

Class Variables

Class Variables in Python

In Object-Oriented Programming (OOP), a class serves as a blueprint for creating objects (instances). Class variables are attributes that belong to the class itself, not to any specific instance of the class. They are shared among all instances of the class. This contrasts with instance variables, which are unique to each object.

Defining Class Variables

Class variables are defined within the class but outside any methods. They are typically declared at the top of the class definition. Conventionally, they are written in uppercase, though it's not strictly enforced by Python.
Python - class variables example class Dog: species = "Canis familiaris" # Class variable population = 0 #Class Variable def __init__(self, name, breed): self.name = name # Instance variable self.breed = breed # Instance variable Dog.population += 1 #Updating Class variable using class name dog1 = Dog("Buddy", "Golden Retriever") dog2 = Dog("Lucy", "Labrador") print(dog1.species) print(dog2.species) print(Dog.population) print(dog1.population) dog1.species = "New Species" # Modifying class variable through instance print(dog1.species) print(dog2.species) print(Dog.species) Dog.species = "Updated Species" print(dog1.species) print(dog2.species) print(Dog.species)

Output

Canis familiaris Canis familiaris 2 2 New Species Canis familiaris Canis familiaris New Species Updated Species Updated Species
In this example, `species` and `population` are class variables. All `Dog` objects share the same `species` value. If we modify `species` through an instance (like `dog1.species = "New Species"`), it creates a new attribute *for that specific instance only*; it does not affect the class variable itself for other instances. However modifying the class variable using the class name will affect all instances created after the modification.

When to Use Class Variables

Class variables are useful for: Representing properties common to all instances: Like the `species` attribute in the `Dog` class – all dogs belong to the same species. Maintaining counters or shared state: The `population` counter keeps track of the total number of `Dog` objects created. Defining default values: A class variable can provide a default value for an instance variable if it's not explicitly set in the constructor (`__init__`).
Class variable - default value example class Car: default_color = "white" def __init__(self, model, color=None): self.model = model self.color = color or self.default_color # Using class variable as default car1 = Car("Toyota")# color defaults to "white" car2 = Car("Honda", "red") print(car1.color) print(car2.color)

Output

white red

Modifying Class Variables

Care must be taken when modifying class variables. Modifying through an instance creates a new instance attribute, shadowing the class variable for that specific object. To change the class variable for all instances, modify it directly through the class name (`ClassName.variable_name = new_value`). Example with Inheritance: Class variables behave differently with inheritance:
Class variables example with inheritance class Animal: kingdom = "Animalia" class Mammal(Animal): class_type = "Mammal" class Dog(Mammal): pass print(Dog.kingdom) print(Dog.class_type) Dog.kingdom = "New Kingdom" # Modifying through subclass print(Dog.kingdom) print(Mammal.kingdom) print(Animal.kingdom)

Output

Animalia Mammal New Kingdom Animalia Animalia
Subclasses inherit class variables. However modifying them in subclass does not affect parent class.
In summary, class variables provide a mechanism to efficiently manage shared attributes among objects of the same class. Understanding their behavior, especially when combined with inheritance and instance variable shadowing, is critical for writing robust and maintainable OOP code in Python.

Tutorials